//
//  TransLaterTableViewCell.m
//  UEAssistant
//
//  Created by mac on 2017/4/14.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "TransLaterTableViewCell.h"
#import "StarsView.h"
#import "TransModel.h"
#import "SelectedModel.h"
#import "AssignModel.h"

@interface TransLaterTableViewCell (){

    NSInteger       _level;
    NSIndexPath     *_cellIndexPath;  // 当前Cell的下标

}

@property (nonatomic, strong) UIImageView  *headView;
@property (nonatomic, strong) UILabel      *name;
@property (nonatomic, strong) StarsView    *starView;
@property (nonatomic ,strong) UIButton     *moreBtn;
@property (nonatomic, strong) UILabel      *translater;
@property (nonatomic, strong) UIView       *line;
@property (nonatomic, strong) UILabel      *workType;
@property (nonatomic, strong) UIImageView  *languageView;
@property (nonatomic, strong) UILabel      *languageLabel;
@property (nonatomic, strong) UILabel      *wordCountLabel;

@property (nonatomic, strong) UILabel                                             *tagLabel;
@property (nonatomic, strong) UILabel                                             *tagLabeltwo;
@property (nonatomic, strong) UILabel                                             *tagLabelThr;
@property (nonatomic, strong) UILabel                                             *tagLabelFour;

@property (nonatomic, assign) BOOL              selected_trans;

@property (nonatomic, strong) UIImageView       *seletImage;
@property (nonatomic, strong) SelectedModel     *model;

@end

@implementation TransLaterTableViewCell

- (void)dealloc{

    [_model removeObserver:self forKeyPath:@"isSelected" context:nil];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUI];
        _model = [[SelectedModel alloc]init];
        [_model addObserver:self forKeyPath:@"isSelected" options:NSKeyValueObservingOptionNew context:nil];
        }
    return self;
}

- (void)setUI{

    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.seletImage];
    
    _headView = [[UIImageView alloc]initWithFrame:CGRectMake(50*Radio, 20*Radio, 50*Radio, 50*Radio)];
    _headView.layer.cornerRadius = 25*Radio;
    _headView.layer.masksToBounds = YES;
    _headView.layer.shouldRasterize = YES; //圆角缓存
    _headView.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
    _headView.image = [UIImage imageNamed:@"UE_logo"];
    [self addSubview:_headView];
    
    _name = [[UILabel alloc]initWithFrame:CGRectMake(_headView.frame.origin.x+_headView.frame.size.width+25, 20*Radio, 200, 20)];
    _name.backgroundColor = [UIColor whiteColor];
    _name.textColor = [UIColor blackColor];
    _name.font = [UIFont systemFontOfSize:FLEXIBLE(15)];
    _name.text = @"正在加载...";
    [self addSubview:_name];
    
    _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_moreBtn setImage:[UIImage imageNamed:@"more_2"] forState:UIControlStateNormal];
    _moreBtn.frame = CGRectMake(Screen_Width-30*Radio, 35*Radio, 20*Radio, 20*Radio);
    _moreBtn.backgroundColor = [UIColor whiteColor];
    [_moreBtn addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_moreBtn setEnlargeEdgeWithTop:30 right:20 bottom:20 left:30];
    [self addSubview:_moreBtn];
    
    _translater = [[UILabel alloc]initWithFrame:CGRectMake(_name.frame.origin.x,_name.frame.origin.y+_name.frame.size.height+20*Radio+18*Radio, 40, 20)];
    _translater.backgroundColor = [UIColor whiteColor];
    _translater.textColor = RGB(163, 174, 185, 1);
    _translater.font = [UIFont systemFontOfSize:FLEXIBLE(15)];
//    _translater.text = @"译员";
    _translater.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_translater];
    
    _line = [[UIView alloc]initWithFrame:CGRectMake(_translater.frame.origin.x+_translater.frame.size.width+10, _translater.frame.origin.y, 1, _translater.frame.size.height)];
    _line.backgroundColor = RGB(163, 174, 185, 1);
    [self addSubview:_line];
    
    _workType = [[UILabel alloc]initWithFrame:CGRectMake(_line.frame.origin.x+10,_line.frame.origin.y, 100, 20)];
    _workType.backgroundColor = [UIColor whiteColor];
    _workType.textColor = RGB(163, 174, 185, 1);
    _workType.font = [UIFont systemFontOfSize:FLEXIBLE(15)];
//    _workType.text = @"兼职";
    _workType.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_workType];

    _languageView = [[UIImageView alloc]initWithFrame:CGRectMake(_translater.frame.origin.x, _translater.frame.origin.y+_translater.frame.size.height+10*Radio, 20*Radio, 20*Radio)];
    _languageView.image = [UIImage imageNamed:@"language"];
    [self addSubview:_languageView];
    
    _languageLabel = [[UILabel alloc]initWithFrame:CGRectMake(_languageView.frame.origin.x+_languageView.frame.size.width+10, _languageView.frame.origin.y-5*Radio, 220*Radio, 30*Radio)];
    _languageLabel.backgroundColor = [UIColor whiteColor];
    _languageLabel.textColor = RGB(130, 138, 154, 1);
    _languageLabel.textAlignment = NSTextAlignmentLeft;
    _languageLabel.font = [UIFont systemFontOfSize:FLEXIBLE(16)];
    [self addSubview:_languageLabel];
    
    _wordCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(Screen_Width - 110*Radio, _languageView.frame.origin.y+_languageView.frame.size.height+5*Radio, 100*Radio, 30*Radio)];
    _wordCountLabel.backgroundColor = [UIColor whiteColor];
    _wordCountLabel.textColor = RGB(130, 138, 154, 1);
    _wordCountLabel.textAlignment = NSTextAlignmentRight;
    _wordCountLabel.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
    [self addSubview:_wordCountLabel];
    
    _tagLabel = [[UILabel alloc]initWithFrame:CGRectMake(_languageView.frame.origin.x, _languageView.frame.origin.y+_languageView.frame.size.height+5*Radio, 50*Radio, 20*Radio)];
    _tagLabel.backgroundColor = [UIColor whiteColor];
    _tagLabel.textColor = RGB(58, 180, 236, 1);
    _tagLabel.textAlignment = NSTextAlignmentCenter;
    _tagLabel.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
    _tagLabel.layer.cornerRadius = 10*Radio;
    _tagLabel.layer.masksToBounds = YES;
    _tagLabel.layer.borderColor = RGB(130, 202, 232, 1).CGColor;
    _tagLabel.layer.borderWidth = 1;
    _tagLabel.layer.shouldRasterize = YES; //圆角缓存
    _tagLabel.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
    [self addSubview:_tagLabel];
    
    _tagLabeltwo = [[UILabel alloc]initWithFrame:CGRectMake(_tagLabel.frame.origin.x+_tagLabel.frame.size.width+10*Radio, _languageView.frame.origin.y+_languageView.frame.size.height+5*Radio, 50*Radio, 20*Radio)];
    _tagLabeltwo.backgroundColor = [UIColor whiteColor];
    _tagLabeltwo.textColor = RGB(58, 180, 236, 1);
    _tagLabeltwo.textAlignment = NSTextAlignmentCenter;
    _tagLabeltwo.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
    _tagLabeltwo.layer.cornerRadius = 10*Radio;
    _tagLabeltwo.layer.masksToBounds = YES;
    _tagLabeltwo.layer.borderColor = RGB(130, 202, 232, 1).CGColor;
    _tagLabeltwo.layer.borderWidth = 1;
    _tagLabeltwo.layer.shouldRasterize = YES; //圆角缓存
    _tagLabeltwo.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
    [self addSubview:_tagLabeltwo];
    
    _tagLabelThr = [[UILabel alloc]initWithFrame:CGRectMake(_tagLabeltwo.frame.origin.x+_tagLabeltwo.frame.size.width+10*Radio, _languageView.frame.origin.y+_languageView.frame.size.height+5*Radio, 50*Radio, 20*Radio)];
    _tagLabelThr.backgroundColor = [UIColor whiteColor];
    _tagLabelThr.textColor = RGB(58, 180, 236, 1);
    _tagLabelThr.textAlignment = NSTextAlignmentCenter;
    _tagLabelThr.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
    _tagLabelThr.layer.cornerRadius = 10*Radio;
    _tagLabelThr.layer.masksToBounds = YES;
    _tagLabelThr.layer.borderColor = RGB(130, 202, 232, 1).CGColor;
    _tagLabelThr.layer.borderWidth = 1;
    _tagLabelThr.layer.shouldRasterize = YES; //圆角缓存
    _tagLabelThr.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
    [self addSubview:_tagLabelThr];
    
    _tagLabelFour = [[UILabel alloc]initWithFrame:CGRectMake(_tagLabelThr.frame.origin.x+_tagLabelThr.frame.size.width+10*Radio, _languageView.frame.origin.y+_languageView.frame.size.height+5*Radio, 50*Radio, 20*Radio)];
    _tagLabelFour.backgroundColor = [UIColor whiteColor];
    _tagLabelFour.textColor = RGB(58, 180, 236, 1);
    _tagLabelFour.textAlignment = NSTextAlignmentCenter;
    _tagLabelFour.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
    _tagLabelFour.layer.cornerRadius = 10*Radio;
    _tagLabelFour.layer.masksToBounds = YES;
    _tagLabelFour.layer.borderColor = RGB(130, 202, 232, 1).CGColor;
    _tagLabelFour.layer.borderWidth = 1;
    _tagLabelFour.layer.shouldRasterize = YES; //圆角缓存
    _tagLabelFour.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
    [self addSubview:_tagLabelFour];

    
    [_translater setHidden:YES];
    [_workType setHidden:YES];
    [_line setHidden:YES];
    [_languageView setHidden:YES];
    [_languageLabel setHidden:YES];
    [_tagLabel setHidden:YES];
    [_tagLabeltwo setHidden:YES];
    [_tagLabelThr setHidden:YES];
    [_tagLabelFour setHidden:YES];
    [_wordCountLabel setHidden:YES];

}

- (void)starLevel:(NSInteger)level andCellIndexPath:(NSIndexPath *)indexPath{

    _cellIndexPath = indexPath;
    _level = level;
//    [self addSubview:self.starView];
}

- (void)moreButtonClicked{

    //show more info
    self.moreBtn.selected = !self.moreBtn.selected;
    if (self.moreBtn.selected) {
        [self.moreBtn setImage:[UIImage imageNamed:@"unfold"] forState:UIControlStateNormal];
        [_translater setHidden:NO];
        [_workType setHidden:NO];
        [_line setHidden:NO];
        [_languageView setHidden:NO];
        [_languageLabel setHidden:NO];
        [_tagLabel setHidden:NO];
        [_tagLabeltwo setHidden:NO];
        [_tagLabelThr setHidden:NO];
        [_tagLabelFour setHidden:NO];
        [_wordCountLabel setHidden:NO];
    }else{
        [self.moreBtn setImage:[UIImage imageNamed:@"more_2"] forState:UIControlStateNormal];
        [_translater setHidden:YES];
        [_workType setHidden:YES];
        [_line setHidden:YES];
        [_languageView setHidden:YES];
        [_languageLabel setHidden:YES];
        [_tagLabel setHidden:YES];
        [_tagLabeltwo setHidden:YES];
        [_tagLabelThr setHidden:YES];
        [_tagLabelFour setHidden:YES];
        [_wordCountLabel setHidden:YES];
    }
    // 记录当前按钮的选中状态，并传递给Controller
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:[NSNumber numberWithInteger:_cellIndexPath.row] forKey:@"row"];
    [dic setObject:[NSNumber numberWithBool:self.moreBtn.selected] forKey:@"isShow"];
    
    // 协议回调，改变Controller中存放Cell展开收起状态的字典
    if (self.delegate && [self.delegate respondsToSelector:@selector(remarksCellShowContrntWithDic:andCellIndexPath:)]) {
        [self.delegate remarksCellShowContrntWithDic:dic andCellIndexPath:_cellIndexPath];
    }

}

-(void)accessData:(id)model{
    
    if ([model isKindOfClass:[AssignModel class]]) {
        
        AssignModel *dataModel = model;
        if (!dataModel.groupId) {
            dataModel.groupId = @"xxx";
        }
        [_headView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",headImageViewOSS,dataModel.facePath]] placeholderImage:[UIImage imageNamed:@"user"]];
        if (![dataModel.userRemark isEqualToString:@""] && dataModel.userRemark) {
            _name.text = dataModel.userRemark;
        }else{
            _name.text = dataModel.nickName;
        }
        
        NSArray *strArr = [dataModel.languageValue componentsSeparatedByString:@"_"];
        _languageLabel.text = [NSString stringWithFormat:@"%@ -> %@",strArr[0],strArr[1]];
        if (dataModel.wordretain1) {
            _wordCountLabel.text = [NSString stringWithFormat:@"%@字",dataModel.wordretain1];
        }
        else{
            _wordCountLabel.text = @"0字";
        }
        if ([dataModel.taskType intValue] == 0) {
            _translater.text = @"超级管理员";
        }else if ([dataModel.taskType intValue] == 1){
            _translater.text = @"译员";
        }else if ([dataModel.taskType intValue] == 4){
            _translater.text = @"项目经理";
        }
        
        if (dataModel.workType) {
            if ([dataModel.workType intValue] == 0) {
                _workType.text = @"全职";
            }else if ([dataModel.workType intValue] == 1){
                _workType.text = @"兼职";
            }else{
                _workType.text = @"未指定";
            }
        }else{
            _workType.text = @"未指定";
        }
        
        CGSize size = [_translater.text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:FLEXIBLE(15)]}];
        CGSize sizeNew = CGSizeMake(ceilf(size.width), ceilf(size.height));
        _translater.frame = CGRectMake(_translater.frame.origin.x, _translater.frame.origin.y, sizeNew.width, 20);
        _line.frame = CGRectMake(_translater.frame.origin.x+_translater.frame.size.width+10, _translater.frame.origin.y, 1, _translater.frame.size.height);
        _workType.frame = CGRectMake(_line.frame.origin.x+10,_line.frame.origin.y, 100, 20);
        
        NSArray *types = [dataModel.labelStr componentsSeparatedByString:@","];
        NSInteger index = types.count;
        if (index == 3) {
            _tagLabel.text       = types[0];
            _tagLabeltwo.text    = types[1];
            _tagLabelThr.text    = types[2];
            [_tagLabelFour removeFromSuperview];
        }
        else if (index == 2){
            _tagLabel.text       = types[0];
            _tagLabeltwo.text    = types[1];
            [_tagLabelThr removeFromSuperview];
            [_tagLabelFour removeFromSuperview];
        }
        else if (index == 1){
            _tagLabel.text       = types[0];
            [_tagLabeltwo removeFromSuperview];
            [_tagLabelThr removeFromSuperview];
            [_tagLabelFour removeFromSuperview];
    
        }
        else if (index == 0){
            [_tagLabel removeFromSuperview];
            [_tagLabeltwo removeFromSuperview];
            [_tagLabelThr removeFromSuperview];
            [_tagLabelFour removeFromSuperview];
    
        }
        else{
            _tagLabel.text       = types[0];
            _tagLabeltwo.text    = types[1];
            _tagLabelThr.text    = types[2];
            _tagLabelFour.text   = @"...";
        }
        NSLog(@"types = %@",types);
        if (types.count == 0 || [types[0] isEqualToString:@""]) {
            [_tagLabel removeFromSuperview];
        }
    
        if (types.count > 0) {
            CGSize size = [[NSString stringWithFormat:@"%@**",types[0]] sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:FLEXIBLE(13)]}];
            CGSize sizeNew = CGSizeMake(ceilf(size.width), ceilf(size.height));
            _tagLabel.frame = CGRectMake(_languageView.frame.origin.x, _languageLabel.frame.origin.y+_languageLabel.frame.size.height+5*Radio, sizeNew.width, 20*Radio);
        }
        if (types.count > 1) {
            CGSize sizetwo = [[NSString stringWithFormat:@"%@**",types[1]] sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:FLEXIBLE(13)]}];
            CGSize sizeNewtwo = CGSizeMake(ceilf(sizetwo.width), ceilf(sizetwo.height));
            _tagLabeltwo.frame = CGRectMake(_tagLabel.frame.origin.x+_tagLabel.frame.size.width+10*Radio, _languageLabel.frame.origin.y+_languageLabel.frame.size.height+5*Radio, sizeNewtwo.width, 20*Radio);
        }
        if (types.count > 2) {
            CGSize sizethr = [[NSString stringWithFormat:@"%@**",types[2]] sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:FLEXIBLE(13)]}];
            CGSize sizeNewthr = CGSizeMake(ceilf(sizethr.width), ceilf(sizethr.height));
            _tagLabelThr.frame = CGRectMake(_tagLabeltwo.frame.origin.x+_tagLabeltwo.frame.size.width+10*Radio, _languageLabel.frame.origin.y+_languageLabel.frame.size.height+5*Radio, sizeNewthr.width, 20*Radio);
        }
        if (types.count > 3) {
            [_tagLabelFour removeFromSuperview];
        }
        if (types.count > 2) {
            if ([NSString stringWithFormat:@"%@%@%@",types[0],types[1],types[2]].length > 8) {
                [_tagLabelThr removeFromSuperview];
            }
        }
    }
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
        for (UIControl *control in self.subviews){
            if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
                for (UIView *view in control.subviews)
                {
                    if ([view isKindOfClass: [UIImageView class]]) {
                        UIImageView *image=(UIImageView *)view;
                        [ShareCenter shareModelData].selectIcon_frame = image.frame;
                        if (self.selected) {
                            image.image=[UIImage imageNamed:@"Selected"];
                        }else{
                            image.image = [UIImage imageNamed:@"cancel"];
                        }
                    }
                }
            }
        }
}

- (StarsView *)starView{

    if (!_starView) {
        _starView = [[StarsView alloc]initWithFrame:CGRectMake(_headView.frame.origin.x+_headView.frame.size.width+25, _name.frame.origin.y+_name.frame.size.height+10*Radio, 90*Radio, 18*Radio) level:_level];
    }
    return _starView;
}

- (UIImageView *)seletImage{

    if (!_seletImage) {
        _seletImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20*Radio, 20*Radio)];
        _seletImage.backgroundColor = [UIColor clearColor];
        _seletImage.center = CGPointMake(22*Radio, 90*Radio/2);
    }
    return _seletImage;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

//    if ([keyPath isEqualToString:@"isSelected"]) {
//    
//        if (_model.isSelected) {
//            _seletImage.image = [UIImage imageNamed:@"Selected"];
//        }else{
//            _seletImage.image = [UIImage imageNamed:@"cancel"];·
    
//        }
//    }
}


@end


